using System.Net;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using SmartBoard.Core.BaseObjects;

namespace SmartBoard.Core.Networking
{
    /// <summary>
    /// Connection creator using in BaseSocketConnectionHost.
    /// </summary>
    public abstract class BaseSocketConnectionCreator : Disposable, IBaseSocketConnectionCreator
    {
        #region Constructor

        protected BaseSocketConnectionCreator(BaseSocketConnectionHost host, string name, IPEndPoint localEndPoint, EncryptionType encryptionType, CompressionType compressionType, ICryptoService cryptoService)
        {
            Host = host;
            Name = name;
            InternalLocalEndPoint = localEndPoint;
            CompressionType = compressionType;
            EncryptionType = encryptionType;
            CryptoService = cryptoService;
        }

        #endregion

        #region Destructor

        protected override void Free(bool canAccessFinalizable)
        {
            InternalLocalEndPoint = null;
            CryptoService = null;
            Host = null;
            base.Free(canAccessFinalizable);
        }

        #endregion

        #region Methods

        internal bool ValidateServerCertificateCallback(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
        {
            bool acceptCertificate = false;
            CryptoService.OnSSLClientValidateServerCertificate(certificate, chain, sslPolicyErrors, out acceptCertificate);
            return acceptCertificate;
        }

        #endregion

        #region Properties

        internal BaseSocketConnectionHost Host
        {
            get; private set;
        }

        public string Name
        {
            get; private set;
        }

        public ICryptoService CryptoService
        {
            get; private set;
        }

        public EncryptionType EncryptionType
        {
            get; private set;
        }

        internal IPEndPoint InternalLocalEndPoint
        {
            get; set;
        }

        public CompressionType CompressionType
        {
            get; private set;
        }

        #endregion

        #region Abstract Methods
        public abstract void Start();
        public abstract void Stop();
        #endregion
    }
}